﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Grinder.Fb2;

namespace Grinder.Renamer.RenamePlugins
{
    internal sealed class Fb2RenamePlugin : IRenamePlugin
    {
        private readonly Dictionary<string, string> _tokens;
        public string Name { get { return "FB2 Files"; } }
        public IEnumerable<string> Extensions { get { return new List<string> { "*.FB2" }; } }
        public Dictionary<string, string> Tokens { get { return _tokens; } }
        public System.Drawing.Image Icon { get { return Properties.Resources.book_open; } }
        public override string ToString() { return string.Format("{0} ({1})", Name, string.Join(",", Extensions.ToArray()).ToLower()); }

        public Fb2RenamePlugin()
        {
            _tokens = new Dictionary<string, string>
                         {
                             {"authorfirst", "Author first name"},
                             {"authorlast", "Author last name"},
                             {"authormiddle", "Author middle name"},
                             {"title", "Book title"},
                             {"bookdate", "Date"},
                             {"genre", "Genre"},
                             {"lang", "Language"},
                             {"srclang", "Source language"},
                             {"seqname", "Sequence name"},
                             {"seqnum", "Number of book in sequence"},
                             {"docdate", "Document date"},
                             {"id", "Document ID"},
                             {"program", "Program used"},
                             {"sourceocr", "Source OCR"},
                             {"version", "Document version"},
                             {"bookname", "Published book name"},
                             {"city", "Publish city"},
                             {"publisher", "Publisher name"},
                             {"publishyear", "Publish year"}
                         };
        }

        public Dictionary<string, object> GetTokens(FileInfo file)
        {
            var book = new FictionBook(file);

            var dic = new Dictionary<string, object>();

            #region TitleInfo
            if (book.TitleInfo.Authors.Count > 0)
            {
                dic.Add("authorfirst", book.TitleInfo.Authors[0].FirstName);
                dic.Add("authorlast", book.TitleInfo.Authors[0].LastName);                
                dic.Add("authormiddle", book.TitleInfo.Authors[0].MiddleName);
            }

            if (book.TitleInfo.Genres.Count > 0)
                dic.Add("genre", book.TitleInfo.Genres[0]);

            if (book.TitleInfo.Sequences.Count > 0)
            {
                dic.Add("seqname", book.TitleInfo.Sequences[0].Name);
                dic.Add("seqnum", book.TitleInfo.Sequences[0].Number);
            }

            dic.Add("title", book.TitleInfo.BookTitle);
            dic.Add("bookdate", book.TitleInfo.Date);
            dic.Add("lang", book.TitleInfo.Language);
            dic.Add("srclang", book.TitleInfo.SrcLanguage);
            #endregion

            #region DocumentInfo
            dic.Add("docdate", book.DocumentInfo.Date);
            dic.Add("id", book.DocumentInfo.Id);
            dic.Add("program", book.DocumentInfo.ProgramUsed);
            dic.Add("sourceocr", book.DocumentInfo.SourceOcr);
            dic.Add("version", book.DocumentInfo.Version);
            #endregion

            #region PublishInfo
            dic.Add("bookname", book.PublishInfo.BookName);
            dic.Add("city", book.PublishInfo.City);
            dic.Add("publisher", book.PublishInfo.Publisher);
            dic.Add("publishyear", book.PublishInfo.Year);
            #endregion

            return dic;
        }
    }
}